﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SQLEditor.admin.entity;
using SQLEditor.admin.service;

namespace SQLEditor.admin.ashx
{
    /// <summary>
    /// rm_sqlanswer 的摘要说明
    /// </summary>
    public class rm_sqlanswer :rm_base
    {
        SqlAnswerService bs = new SqlAnswerService();
        public override void getall(HttpContext context)
        {
            string json = bs.getall();
            context.Response.Write(json);
        }

        public override void search(HttpContext context)
        {
            string strWhere = "("+getWhere(context)+")";

            string sort = context.Request.Params["sort"] ?? "id";   //排序列
            string order = context.Request.Params["order"] ?? "asc";     //排序方式 asc或者desc
            int pageIndex = int.Parse(context.Request.Params["page"] ?? "1");
            int pageSize = int.Parse(context.Request.Params["rows"] ?? "20");

            string json = bs.search(pageSize, pageIndex, sort + " " + order, strWhere);
            context.Response.Write(json);

        }
        private string getWhere(HttpContext context) {
            string where = "1=1";
            string username = context.Request.Params["username"];
            string deptname = context.Request.Params["deptname"];
            string questionname = context.Request.Params["questionname"];
            string status = context.Request.Params["status"];
            string answerdatestart = context.Request.Params["answerdatestart"];
            string answerdateend = context.Request.Params["answerdateend"];
            UserEntity user = UserService.GetUser(context);
            if (user.roleNames.Contains("学生"))
            {
                where += " and userId=" + user.id;
            }
            if (!string.IsNullOrEmpty(username) || !string.IsNullOrEmpty(deptname))
            {
                where += " and userId in( select u.id from tbUser u, tbDepartment d, tbUserDepartment ud where u.id=ud.userId and ud.deptId=d.id";
                
                if (!string.IsNullOrEmpty(username))
                {
                    where += " and realName like '%" + username + "%'";
                }
                if (!string.IsNullOrEmpty(deptname))
                {
                    where += " and name like '%" + deptname + "%'";
                }
                where += ")";
            }

            if (!string.IsNullOrEmpty(questionname))
            {
                where += " and questionId in (select id from sqlQuestion where title like '%"+questionname+"%')";		
            }
            if (!string.IsNullOrEmpty(status) && status!="select")
            {
                where += " and status="+status;
            }
            if (!string.IsNullOrEmpty(answerdatestart))
            {
                where += " and submittime > '" + answerdatestart + "'";
            }
            if (!string.IsNullOrEmpty(answerdateend))
            {
                where += " and submittime<'" + answerdateend + "'";
            }
            return where;
        }

       
        public override void delete(HttpContext context)
        {
            string ids = context.Request.Params["ids"];
            string json = bs.delete(ids);
            context.Response.Write(json);
        }
    }
}